package net.lovesrc.allin.jobs.backup;

import com.alibaba.fastjson.JSONObject;
import net.lovesrc.allin.biz.stl.StlRegisterService;
import net.lovesrc.allin.biz.stl.StlService;
import net.lovesrc.allin.jobs.base.BaseJob;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;

import java.util.Date;
import java.util.List;

//@Component
@Profile("live")
public class StlAuthIdJob extends BaseJob {

    @Autowired
    private StlRegisterService stlRegisterService;

    @Autowired
    private StlService stlService;

    @Override
    public void runOnce() {
        // 实名
        {
            JSONObject row = queryForRow(jdbcTemplate, "select mobile from t_stl_account where idStatus is null and insertTime < ? order by rand() limit 1",
                    DateUtils.addMinutes(new Date(), -5)
            );
            if (row != null) {
                String mobile = row.getString("mobile");
                stlRegisterService.authId(mobile);
            }
        }
        // 同步实名结果
        {
            JSONObject row = queryForRow(jdbcTemplate, "select mobile from t_stl_account where idStatus = 'submitted' and idResError = 0 and idTime < ? order by rand() limit 1",
                    DateUtils.addSeconds(new Date(), -30)
            );
            if (row != null) {
                String mobile = row.getString("mobile");
                execute("STL", mobile, () -> stlRegisterService.syncStatus(mobile));
            }
        }
        {
            List<JSONObject> rows = queryForRows(jdbcTemplate, "select * from t_stl_account where worked = 1 and isMyAccount = 0 and alipay is null");
            for (JSONObject row : rows) {
                stlService.resetAlipay(row.getString("mobile"));
            }
        }
    }

}
